﻿


// Author:					Doan Quoc Tuan
// Created:					2013-5-22
// Last Modified:			2013-5-22
// 
// The use and distribution terms for this software are covered by the 
// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)  
// which can be found in the file CPL.TXT at the root of this distribution.
// By using this software in any fashion, you are agreeing to be bound by 
// the terms of this license.
//
// You must not remove this notice, or any other, from this software.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlTypes;
using SMSPanasonic.Data;

namespace SMSPanasonic.Business
{

    public class SmsMtReport
    {

        #region Constructors

        public SmsMtReport()
        { }


        public SmsMtReport(
            SqlGuid mtGuid)
        {
            GetSmsMtReport(
                mtGuid);
        }

        #endregion

        #region Private Properties

        private SqlGuid _mtGuid = SqlGuid.Null;
        private SqlDateTime _sentTime = SqlDateTime.Null;
        private SqlString _status = SqlString.Null;
        private SqlDateTime _reportTime = SqlDateTime.Null;
        private SqlString _reportStatus = SqlString.Null;

        #endregion

        #region Public Properties

        public SqlGuid MtGuid
        {
            get { return _mtGuid; }
            set { _mtGuid = value; }
        }
        public SqlDateTime SentTime
        {
            get { return _sentTime; }
            set { _sentTime = value; }
        }
        public SqlString Status
        {
            get { return _status; }
            set { _status = value; }
        }
        public SqlDateTime ReportTime
        {
            get { return _reportTime; }
            set { _reportTime = value; }
        }
        public SqlString ReportStatus
        {
            get { return _reportStatus; }
            set { _reportStatus = value; }
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Gets an instance of SmsMtReport.
        /// </summary>
        /// <param name="mtGuid"> mtGuid </param>
        private void GetSmsMtReport(
            SqlGuid mtGuid)
        {
            using (IDataReader reader = DBSmsMtReport.GetOne(
                mtGuid))
            {
                PopulateFromReader(reader);
            }

        }


        private void PopulateFromReader(IDataReader reader)
        {
            if (reader.Read())
            {
                if (!reader.IsDBNull(0)) this._mtGuid = reader.GetGuid(0);
                if (!reader.IsDBNull(1)) this._sentTime = reader.GetDateTime(1);
                if (!reader.IsDBNull(2)) this._status = reader.GetString(2);
                if (!reader.IsDBNull(3)) this._reportTime = reader.GetDateTime(3);
                if (!reader.IsDBNull(4)) this._reportStatus = reader.GetString(4);

            }

        }

        /// <summary>
        /// Persists a new instance of SmsMtReport. Returns true on success.
        /// </summary>
        /// <returns></returns>
        private bool Create()
        {
            this._mtGuid = Guid.NewGuid();

            int rowsAffected = DBSmsMtReport.Create(
                this._mtGuid,
                this._sentTime,
                this._status,
                this._reportTime,
                this._reportStatus);

            return (rowsAffected > 0);

        }


        /// <summary>
        /// Updates this instance of SmsMtReport. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
        private bool Update()
        {

            return DBSmsMtReport.Update(
                this._mtGuid,
                this._sentTime,
                this._status,
                this._reportTime,
                this._reportStatus);

        }





        #endregion

        #region Public Methods

        /// <summary>
        /// Saves this instance of SmsMtReport. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
        public bool Save()
        {
            if (this._mtGuid != Guid.Empty)
            {
                return Update();
            }
            else
            {
                return Create();
            }
        }




        #endregion

        #region Static Methods

        /// <summary>
        /// Deletes an instance of SmsMtReport. Returns true on success.
        /// </summary>
        /// <param name="mtGuid"> mtGuid </param>
        /// <returns>bool</returns>
        public static bool Delete(
            SqlGuid mtGuid)
        {
            return DBSmsMtReport.Delete(
                mtGuid);
        }


        /// <summary>
        /// Gets a count of SmsMtReport. 
        /// </summary>
        public static int GetCount(string sWhereCondition)
        {
            return DBSmsMtReport.GetCount(sWhereCondition);
        }

        private static List<SmsMtReport> LoadListFromReader(IDataReader reader)
        {
            List<SmsMtReport> _smsMtReportList = new List<SmsMtReport>();
            try
            {
                while (reader.Read())
                {
                    SmsMtReport _smsMtReport = new SmsMtReport();
                    if (!reader.IsDBNull(0)) _smsMtReport._mtGuid = reader.GetGuid(0);

                    if (!reader.IsDBNull(1)) _smsMtReport._sentTime = reader.GetDateTime(1);

                    if (!reader.IsDBNull(2)) _smsMtReport._status = reader.GetString(2);

                    if (!reader.IsDBNull(3)) _smsMtReport._reportTime = reader.GetDateTime(3);

                    if (!reader.IsDBNull(4)) _smsMtReport._reportStatus = reader.GetString(4);

                    _smsMtReportList.Add(_smsMtReport);

                }
            }
            finally
            {
                reader.Close();
            }

            return _smsMtReportList;

        }

        /// <summary>
        /// Gets an IList with all instances of SmsMtReport.
        /// </summary>
        public static List<SmsMtReport> GetAll()
        {
            IDataReader reader = DBSmsMtReport.GetAll();
            return LoadListFromReader(reader);

        }

        /// <summary>
        /// Gets an IList with page of instances of SmsMtReport.
        /// </summary>
        /// <param name="pageNumber">The page number.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="totalPages">total pages</param>
        public static List<SmsMtReport> GetPage(string sWhereCondition, string sOrderBy, int pageNumber, int pageSize, out int totalPages)
        {
            totalPages = 1;
            IDataReader reader = DBSmsMtReport.GetPage(sWhereCondition, sOrderBy, pageNumber, pageSize, out totalPages);
            return LoadListFromReader(reader);
        }


        public static void Insert(DataTable dtSource)
        {
            DBSQLCommand.BulkCopy("SmsMtReport", dtSource);
        }
        #endregion

        #region Comparison Methods

        /// <summary>
        /// Compares 2 instances of SmsMtReport.
        /// </summary>
        public static int CompareBySentTime(SmsMtReport _smsMtReport1, SmsMtReport _smsMtReport2)
        {
            return _smsMtReport1.SentTime.CompareTo(_smsMtReport2.SentTime);
        }
        /// <summary>
        /// Compares 2 instances of SmsMtReport.
        /// </summary>
        public static int CompareByStatus(SmsMtReport _smsMtReport1, SmsMtReport _smsMtReport2)
        {
            return _smsMtReport1.Status.CompareTo(_smsMtReport2.Status);
        }
        /// <summary>
        /// Compares 2 instances of SmsMtReport.
        /// </summary>
        public static int CompareByReportTime(SmsMtReport _smsMtReport1, SmsMtReport _smsMtReport2)
        {
            return _smsMtReport1.ReportTime.CompareTo(_smsMtReport2.ReportTime);
        }
        /// <summary>
        /// Compares 2 instances of SmsMtReport.
        /// </summary>
        public static int CompareByReportStatus(SmsMtReport _smsMtReport1, SmsMtReport _smsMtReport2)
        {
            return _smsMtReport1.ReportStatus.CompareTo(_smsMtReport2.ReportStatus);
        }

        #endregion


    }

}







